package com.sail.www.util;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.log4j.Logger;

public class Property {

	private static final Logger logger = Logger.getLogger(Property.class);
	private Properties prop;

	public Property(String fileName) {
		InputStream inputStream = null;
		try {
			inputStream = Thread.currentThread().getContextClassLoader()
					.getResourceAsStream(fileName);
			prop.load(inputStream);
		} catch (FileNotFoundException e) {
			logger.error("Properties file " + fileName + " is not found");
		} catch (IOException e) {
			logger.error("Properties file " + fileName + " load error");
		} finally {
			if (inputStream != null) {
				try {
					inputStream.close();
				} catch (IOException e) {
					logger.error("Properties file " + fileName
							+ " close InputStream error");
				}
			}
		}
	}

	public int getValueAsInt(String key) {
		return getValueAsInt(key, 0);
	}

	public int getValueAsInt(String key, int defaultValue) {
		String value = prop.getProperty(key);
		return value == null ? defaultValue : Integer.parseInt(value);
	}

	public long getValueAsLong(String key) {
		return getValueAsLong(key, 0);
	}

	public long getValueAsLong(String key, long defaultValue) {
		String value = prop.getProperty(key);
		return value == null ? defaultValue : Long.parseLong(value);
	}

	public boolean getValueAsBoolean(String key) {
		return getValueAsBoolean(key, false);
	}

	public boolean getValueAsBoolean(String key, boolean defaultValue) {
		String value = prop.getProperty(key);
		return value == null ? defaultValue : Boolean.parseBoolean(value);
	}

	public String getValueAsString(String key) {
		return getValueAsString(key, "");
	}

	public String getValueAsString(String key, String defaultValue) {
		String value = prop.getProperty(key);
		return value == null ? defaultValue : value;
	}

}
